package yr.website

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class RolesController {

    def rolesService
    static final Logger logger = LoggerFactory.getLogger(RolesController.class)

    def getAll() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def name = "${params.name ?: ''}"
        def m = 1
        def c = m.longValue()
        try {
            def a = Roles.createCriteria()
            def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
                and {
                    if (!name.isEmpty()) {
                        eq("name", name)
                    }
                    not { 'in'("id", c) }
                }
            }
            def b = Roles.createCriteria()
            def length = b.get {
                and {
                    if (!name.isEmpty()) {
                        eq("name", name)
                    }
                    not { 'in'("id", c) }
                }
                projections {
                    count("name")
                }
            }
            if (list) {
                data << [statuCode: "0", errMsg: "0", data: list, total: length]
            } else {
                data << [statuCode: "1", errMsg: "1"]
            }
            render data as JSON
        } catch (Exception e) {
            logger.error("RolesController -- getAll Exception " + e.message)
        }
    }

    def ad() {
        try {
            def data = [:]
            def page = Integer.parseInt("${params.page ?: 1}")
            def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
            def name = "${params.name ?: ''}"
            def role = Roles.findByName(name)
            def m = 1
            def c = m.longValue()
            if (role) {
                data << [statuCode: "1", errMsg: "1"]
            } else {
                def roles = new Roles()
                bindData(roles, params, [exclude: ['page', 'pagesize']])
                rolesService.ad(roles)
                def a = Roles.createCriteria()
                def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
                    and {
                        not { 'in'("id", c) }
                    }
                    order("createDate","desc")
                }
                def b = Roles.createCriteria()
                def length = b.get {
                    and {
                        not { 'in'("id", c) }
                    }
                    projections {
                        count("name")
                    }
                }
                new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "添加某种角色").save()
                data << [statuCode: "0", errMsg: "0", data: list, total: length]
            }
            render data as JSON
        } catch (Exception e) {
            logger.error("RolesController -- ad Exception " + e.message)
        }
    }

    def del() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def roleId = "${params.roleId ?: ''}"
        def m = 1
        def c = m.longValue()
        try {
            def role = Roles.findByRoleId(roleId)
            rolesService.del(role)
            def a = Roles.createCriteria()
            def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
                and {
                    not { 'in'("id", c) }
                }
                order("createDate","desc")
            }
            def b = Roles.createCriteria()
            def length = b.get {
                and {
                    not { 'in'("id", c) }
                }
                projections {
                    count("name")
                }
            }
            new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "删除角色名 " + role.name).save()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("RolesController -- del Exception " + e.message)
        }
    }

    def refresh() {
        try {
            def data = [:]
            def page = 1
            def pagesize = 20
            def roleId = "${params.roleId ?: ''}"
            def role = Roles.findByRoleId(roleId)
            def m = 1
            def c = m.longValue()
            bindData(role, params, [exclude: ['roleId']])
            rolesService.save(role)
            def a = Roles.createCriteria()
            def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
                and {
                    not { 'in'("id", c) }
                }
                order("createDate","desc")
            }
            def b = Roles.createCriteria()
            def length = b.get {
                and {
                    not { 'in'("id", c) }
                }
                projections {
                    count("name")
                }
            }
            new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "修改角色 " + role.name).save()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
//            }
            render data as JSON
        } catch (Exception e) {
            logger.error("RolesController -- refresh Exception " + e.message)
        }
    }
}
